import {FrameBuffer} from "../output/FrameBuffer";
import {ZBuffer} from "../output/ZBuffer";
import {Matrix4} from "../math/Matrix4";

export class RenderContext {
    width: number;
    height: number;

    frameBuffer: FrameBuffer;
    zBuffer: ZBuffer;

    canvasMatrix: Matrix4;

    constructor(width: number, height:number) {
        this.width = width;
        this.height = height;
        this.frameBuffer = new FrameBuffer(this.width, this.height);
        this.zBuffer = new ZBuffer(this.width, this.height);
        this.canvasMatrix = new Matrix4().makeScale(this.width / 2 , this.height / -2 , 1).multiply(new Matrix4().makeTranslation(1, -1, 0));
    }

    setSize(width: number, height: number) {
        this.frameBuffer.setSize(width, height);
        this.zBuffer.setSize(width, height);
        this.width = width;
        this.height = height;
    }

    containPosition(x: number,  y: number) {
        return x >= 0 && x < this.width && y>=0 && y<this.height;
    }

    depthTest(x: number, y:number, depth: number) {
        // return true;
        const old = this.zBuffer.getPositionZ(x, y);
        return depth <= old;
    }

    setDepth(x: number, y: number, depth: number) {
        this.zBuffer.setPositionZ(x, y, depth);
    }

    clear() {
        this.frameBuffer.clear();
        this.zBuffer.clear();
    }
}